### REPLICATION FILE -- Analysis
### Lawrence Ezrow, Jonathan Homola & Margit Tavits
### "When Extremism Pays: Policy Positions, Voter
### Certainty, and Party Support in Postcommunist
### Europe"
### Journal of Politics 76: 535-547
### http://dx.doi.org/10.1017/S0022381613001461

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
#install.packages("foreign"); install.packages("xtable"); install.packages("psych")
#install.packages("sandwich"); install.packages("stargazer"); install.packages("lmtest")
library("foreign"); library("xtable"); library("psych")
library("sandwich"); library("stargazer"); library("lmtest")

## function to calculate STATA-style clustered standard errors
cl <- function(dat,fm, cluster){
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL)}

## set working directory
#setwd(" ... ")

## read in the main dataset 
jop <- read.dta("EzrowHomolaTavits_WhenExtremismPays_Data.dta")
jop_pc <- subset(jop, postcom==1)
jop_ad <- subset(jop, advind==1)



####
#### Table 1 Descriptive Statistics for the Dependent
#### and Independent Variables
####
xtable(describe(jop[,c("nvs", "dist", "reldista", 
                       "party_pro", "party_stddev")])[,c(3,4)], digits=2)

xtable(describe(jop_pc[,c("nvs", "dist", "reldista", 
                          "party_pro", "party_stddev")])[,c(3,4)], digits=2)

xtable(describe(jop_ad[,c("nvs", "dist", "reldista", 
                          "party_pro", "party_stddev")])[,c(3,4)], digits=2)



####
#### Table 2 The Effect of Party Policy 
#### Distance on Normalized Vote Share
####
mod1 <- lm(nvs ~ dist, data=jop_ad)
mod2 <- lm(nvs ~ reldista, data=jop_ad)
mod3 <- lm(nvs ~ dist, data=jop_pc)
mod4 <- lm(nvs ~ reldista, data=jop_pc)

## clustered SEs
mod1 <- cl(jop_ad, mod1, jop_ad$electid)
mod2 <- cl(jop_ad, mod2, jop_ad$electid)
mod3 <- cl(jop_pc, mod3, jop_pc$electid)
mod4 <- cl(jop_pc, mod4, jop_pc$electid)

stargazer(mod1, mod2, mod3, mod4,
          omit.stat=c("f", "ser", "bic", "ll"), 
          star.cutoffs = c(0.1, 0.05, 0.01),
          column.sep.width="1pt", digits=2,
          dep.var.labels.include = FALSE,
          dep.var.caption="Outcome variable: Normalized Vote Share")



####
#### Figure 1 Party Policy Distance and Vote Share
####
####
mod1 <- lm(avs ~ dist, data=jop_ad)
x1 <- c(min(jop_ad$dist), max(jop_ad$dist))
y1 <- predict(mod1, newdata=data.frame(dist=x1))
mod2 <- lm(avs ~ dist, data=jop_pc)
x2 <- c(min(jop_pc$dist), max(jop_pc$dist))
y2 <- predict(mod2, newdata=data.frame(dist=x2))
par(mfrow=c(1,2))
plot(jop_ad$dist, jop_ad$avs, xlim=c(0,6), ylim=c(0,50),
     xlab="Party policy distance", ylab="Vote share",
     main="Advanced democracies")
lines(x=x1, y=y1)
plot(jop_pc$dist, jop_pc$avs, xlim=c(0,6), ylim=c(0,50),
     xlab="Party policy distance", ylab="Vote share",
     main="Postcommunist democracies")
lines(x=x2, y=y2)
par(mfrow=c(1,1))



####
#### Table 3 Regression Coefficients for 
#### Party Policy Distance When Estimating 
#### Voter Certainty
####
mod1 <- lm(party_pro ~ dist, data=jop_pc)
mod2 <- lm(party_stddev ~ dist, data=jop_pc)
mod3 <- lm(party_pro ~ dist, data=jop_ad)
mod4 <- lm(party_stddev ~ dist, data=jop_ad)

## clustered SEs
mod1 <- cl(jop_pc, mod1, jop_pc$partyid)
mod2 <- cl(jop_pc, mod2, jop_pc$partyid)
mod3 <- cl(jop_ad, mod3, jop_ad$partyid)
mod4 <- cl(jop_ad, mod4, jop_ad$partyid)

stargazer(mod1, mod2, mod3, mod4,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.1, 0.05, 0.01),
          column.sep.width="1pt", digits=2,
          dep.var.labels.include = FALSE,
          dep.var.caption="Outcome variable: Normalized Vote Share")



####
#### Table 4 The Effect of Voter Certainty on
#### Normalized Vote Share, All Countries
####
mod1 <- lm(nvs ~ party_pro, data=jop)
mod2 <- lm(nvs ~ party_stddev, data=jop)

## clustered SEs
mod1 <- cl(jop, mod1, jop$electid)
mod2 <- cl(jop, mod2, jop$electid)

stargazer(mod1, mod2,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.1, 0.05, 0.01),
          column.sep.width="1pt", digits=2,
          dep.var.labels.include = FALSE,
          dep.var.caption="Outcome variable: Normalized Vote Share")